from PyPDF2 import PdfFileReader, PdfFileWriter
import fitz
import sys

doc = fitz.open('../demo.pdf')

# 生成图片
for pg in range(1, 3):  # doc.pageCount
    page = doc[pg]
    zoom = int(100)
    rotate = int(0)
    trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate)

    # create raster image of page (non-transparent)
    pm = page.getPixmap(matrix=trans, alpha=False)

    # write a PNG image of the page
    pm.writePNG('%s.png' % pg)

# 截取部分的pdf
def split_pdf(input_file_name, out_file_name):
    pdf_output = PdfFileWriter()
    pdf_input = PdfFileReader(open(input_file_name, 'rb'))
    # 获取 pdf 共用多少页
    page_count = pdf_input.getNumPages()
    print(page_count)
    # 将 pdf 第五页之后的页面，输出到一个新的文件
    for i in range(5, 8):
        pdf_output.addPage(pdf_input.getPage(i))
    pdf_output.write(open(out_file_name, 'wb'))


split_pdf("../demo.pdf", "out.pdf")